home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / rle16_sc.zip / RLE16.C next >
Text File  |  1993-05-10  |  10KB  |  278 lines

  1. /* Run Length Encoding compression program.
  2.  * max sequence is 32767 bytes of either repeating
  3.  * or non-repeating stuff.
  4.  * (uses 16 bit headers.)
  5.  *
  6.  * This program and its source are strictly public domain
  7.  *
  8.  * Written by Shaun Case, 1991, Borland C++ 2.0
  9.  *
  10.  * doesn't do error checking on fputc()
  11.  */
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <dir.h>
  16. #include <string.h>
  17. #include "rle16.h"
  18.  
  19. int main(int argc, char **argv)
  20. {
  21.     register int cur_char;                /* a character                    */
  22.     register unsigned int i;              /* generic index variable         */
  23.     register unsigned short run_len = 0;  /* length of character run so far */
  24.     int run_char;                         /* which char run is of           */
  25.     unsigned int j;                       /* another index variable         */
  26.     unsigned short seq_len=0;             /* length of non-run sequence     */
  27.  
  28.     char seq[MAX_LEN];                    /* buffer for uncompressible data */
  29.     char scratch_space[256];              /* string scratch space           */
  30.  
  31.     char orig_name[MAXFILE+MAXEXT];       /* original filename      */
  32.     char drive[MAXDRIVE],                 /* needed for fnsplit()   */
  33.          dir[MAXDIR],                     /* needed for fnsplit()   */
  34.          filename[MAXFILE],               /* 8 chars, orig name     */
  35.          ext[MAXEXT];                     /* 3 chars, orig ext      */
  36.     char outfile_name[MAXFILE+MAXEXT];    /* output filename        */
  37.  
  38.     unsigned long  orig_total;              /* total number of unencoded bytes          */
  39.     unsigned long  rle_total;               /* total number of encoded bytes            */
  40.     FILE *infile;                           /* file ptr to input file (uncompressed)    */
  41.     FILE *outfile;                          /* file ptr to output file (compressed)     */
  42.     char *infile_name;
  43.  
  44.     fnsplit(argv[1], drive, dir, filename, ext);    /* get filename    */
  45.     strcpy(orig_name, filename);
  46.     strcat(orig_name, ext);
  47.     strcpy(outfile_name, filename);       /* build output filename  */
  48.     strcat(outfile_name, ".r16");
  49.  
  50.     if (argc != 2)
  51.     {
  52.         puts("Usage: RLE16 filename.ext compresses file to filename.r16");
  53.         return 1;
  54.     }
  55.  
  56.     puts("rle16  by Shaun Case 1991  public domain.");
  57.  
  58.     infile_name=argv[1];
  59.  
  60.     if ((infile=fopen(infile_name, "rb")) == NULL)
  61.     {
  62.         strcpy(scratch_space, "Uable to open ");
  63.         strcat(scratch_space, infile_name);
  64.         puts(scratch_space);
  65.         return 1;
  66.     }
  67.     
  68.     if ((outfile=fopen(outfile_name, "wb")) == NULL)
  69.     {
  70.         strcpy(scratch_space, "Uable to open ");
  71.         strcat(scratch_space, outfile_name);
  72.         puts(scratch_space);
  73.         return 1;
  74.     }
  75.  
  76.     for (i = 0; i < 13; i++)            /* write original filename */
  77.         fputc(orig_name[i], outfile);
  78.  
  79.     while (!feof(infile))
  80.     {
  81.         cur_char = fgetc(infile);
  82.  
  83.         if (feof(infile))
  84.             continue;
  85.  
  86.  
  87.         if (seq_len ==0)                /* haven't got a sequence yet   */
  88.         {
  89.             if (run_len == 0)           /* start a new run              */
  90.             {
  91.                 run_char = cur_char;
  92.                 ++run_len;
  93.                 continue;
  94.             }
  95.  
  96.             if (run_char == cur_char)   /* got another char in the run  */
  97.                 if (++run_len == MAX_LEN)
  98.                 {
  99.                     fputc((int)MAX_RUN_HEADER & (0xff), outfile);   /* write lo byte   */
  100.                     fputc((int)MAX_RUN_HEADER >> 8, outfile);       /* write high byte */
  101.                     fputc((int) run_char, outfile);
  102.  
  103.                     run_len = 0;
  104.                     continue;
  105.                 }
  106.  
  107.                                    /* got a different character     */
  108.                                    /* than the run we were building */
  109.             if (run_len > 3)       /* so write out the run and      */
  110.                                    /* start a new one of the new    */
  111.                                    /* character.                    */
  112.             {
  113.                 fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  114.                 fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  115.                 fputc((int)run_char, outfile);
  116.  
  117.                 run_len = 1;
  118.                 run_char   = cur_char;
  119.                 continue;
  120.             }
  121.  
  122.             /* run was only one or two chars, make a seq out of it instead       */
  123.  
  124.             for (j = 0; j < run_len; j++);    /* copy 1 or 2 char run to seq[]   */
  125.             {
  126.                 seq[seq_len] = run_char;
  127.                 ++seq_len;
  128.                 if (seq_len == MAX_LEN)       /* if seq[] is full, write to disk */
  129.                 {
  130.                     fputc((int)MAX_SEQ_HEADER & 0xff, outfile);   /* lo byte */
  131.                     fputc((int)(MAX_SEQ_HEADER >> 8) , outfile);   /* lo byte */
  132.  
  133.                     for (i = 0; i < seq_len; i++)
  134.                         fputc((int)seq[i], outfile);
  135.  
  136.                     seq_len = 0;
  137.                 }
  138.             }
  139.  
  140.             run_len = 0;
  141.  
  142.             seq[seq_len++] = cur_char;
  143.             if (seq_len == MAX_LEN)        /* if seq[] is full, write to disk */
  144.             {
  145.                 fputc((int)MAX_SEQ_HEADER & 0xff, outfile);   /* lo byte */
  146.                 fputc((int)(MAX_SEQ_HEADER >> 8) , outfile);   /* lo byte */
  147.  
  148.                 for (i = 0; i < seq_len; i++)
  149.                     fputc((int)seq[i], outfile);
  150.  
  151.                 seq_len = 0;
  152.             }
  153.         }
  154.         else    /* a sequence exists */
  155.         {
  156.             if (run_len != 0)           /* if a run exists */
  157.             {
  158.                 if (cur_char == run_char )  /* add to run!  Yay.  */
  159.                 {
  160.                     ++run_len;
  161.                     if (run_len == MAX_LEN)  /* if run is full */
  162.                     {
  163.                         /* write sequence that precedes run */
  164.  
  165.                         fputc((int)seq_len & 0xff, outfile);          /* lo byte */
  166.                         fputc((int)((SEQ | seq_len) >> 8), outfile);   /* lo byte */
  167.  
  168.                         for (i = 0; i < seq_len; i++)
  169.                             fputc((int)seq[i], outfile);
  170.  
  171.  
  172.                         /* write run                        */
  173.  
  174.                         fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  175.                         fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  176.                 
  177.                         fputc((int)run_char, outfile);
  178.  
  179.                         /* and start out fresh              */
  180.                         seq_len = run_len = 0;
  181.  
  182.                     }  /* end write full run with existing sequence */
  183.  
  184.                     continue;
  185.  
  186.                 }  /* end add to run for sequence exists */
  187.  
  188.                 /* we couldn't add to the run, and a preceding sequence */
  189.                 /* exists, so write the sequence and the run, and       */
  190.                 /* try starting a new run with the current character.   */
  191.  
  192.                 /* write sequence that precedes run */
  193.  
  194.                 fputc((int)seq_len & 0xff, outfile);          /* lo byte */
  195.                 fputc((int)((SEQ | seq_len) >> 8), outfile);   /* hi byte */
  196.  
  197.                 for (i = 0; i < seq_len; i++)
  198.                     fputc((int)seq[i], outfile);
  199.  
  200.  
  201.                 /* write run                        */
  202.  
  203.                 fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  204.                 fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  205.  
  206.                 fputc((int)run_char, outfile);
  207.  
  208.                 /* and start a new run w/ cur_char  */
  209.                 seq_len = 0;
  210.                 run_len = 1;
  211.                 run_char = cur_char;
  212.  
  213.                 continue;
  214.  
  215.             }    /* end can't add to existing run, and preceding seq exists */
  216.  
  217.             /* no run exists, but a sequences does.  Try to create a run    */
  218.             /* by looking at cur_char and the last char of the sequence.    */
  219.             /* if that fails, add the char to the sequence.                 */
  220.             /* if the sequence is full, write it to disk.  (Slightly non    */
  221.             /* optimal; we could wait one more char.  A small thing to fix  */
  222.             /* if someone gets the urge...                                  */
  223.  
  224.             if (seq[seq_len - 1] == cur_char)       /* if we can make a run */
  225.             {
  226.                 run_char = cur_char;
  227.                 run_len = 2;
  228.                 --seq_len;
  229.                 continue;
  230.             }
  231.  
  232.             /* couldn't make a run, add char to seq.  Maybe next time       */
  233.             /* around...                                                    */
  234.  
  235.             seq[seq_len++] = cur_char;
  236.  
  237.             if (seq_len == MAX_LEN) /* if the sequence is full, write out   */
  238.             {
  239.                 fputc((int)MAX_SEQ_HEADER & 0xff, outfile);   /* lo byte */
  240.                 fputc((int)(MAX_SEQ_HEADER >> 8) , outfile);   /* lo byte */
  241.  
  242.                 for (i = 0; i < MAX_LEN; i++)
  243.                     fputc((int)seq[i], outfile);
  244.  
  245.                 seq_len = 0;
  246.             }
  247.  
  248.         }  /* end branch on sequence exists */
  249.  
  250.     } /* done with whole file */
  251.  
  252.     /* there may be stuff left that hasn't been written yet; if so, write it */
  253.  
  254.     if (seq_len != 0)  /* write sequence that precedes run */
  255.     {
  256.         fputc((int)seq_len & 0xff, outfile);          /* lo byte */
  257.         fputc((int)((SEQ | seq_len) >> 8), outfile);   /* hi byte */
  258.  
  259.         for (i = 0; i < seq_len; i++)
  260.             fputc((int)seq[i], outfile);
  261.     }
  262.  
  263.     if (run_len != 0)  /* write run */
  264.     {
  265.         fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  266.         fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  267.  
  268.         fputc((int)run_char, outfile);
  269.     }
  270.  
  271.     fclose(infile);
  272.     fclose (outfile);
  273.  
  274.     return 0;
  275.  
  276. }  /* end main() */
  277.  
  278.